<?php
include_once 'data.php';
include_once 'functions.php';

if (empty($_SESSION['export_files'])) die('Error! No files to display.');

database_connect($database_path, 'library');
?>
<div id="items-left" class="leftindex noprint alternating_row" style="position:relative;float:left;width:240px;height:100%;overflow:scroll;border:0;margin:0">
    <?php
    if(empty($_GET['file'])) $_GET['file'] = $_SESSION['export_files'][0];
    $key = array_search(intval($_GET['file']), $_SESSION['export_files']);
    $offset = max($key-9, 0);
    if ($offset > count($_SESSION['export_files']) - 20) {
        $offset = max(count($_SESSION['export_files']) - 20, 0);
    }
    $show_items = array_slice($_SESSION['export_files'], $offset, 20);
    if ($offset > 0) {
        print '<div class="ui-state-highlight lib-shadow-bottom" style="margin-bottom:4px;height:17px" title="Previous">';
        print '<a href="items.php?file='.$_SESSION['export_files'][$offset-1].'" class="navigation" style="display:block;width:100%" id="'.$_SESSION['export_files'][$offset-1].'">';
        print '<span class="ui-icon ui-icon-triangle-1-n" style="margin:auto"></span>';
        print '</a></div>';
    }
    print '<div id="list-title-copy" class="items" style="font-weight:bold"></div><div class="separator"></div>';
    $i = 0;
    $divs = array();
    foreach ($show_items as $export_file) {
        $i=$i+1;

        $record = $dbHandle->query("SELECT id,title FROM library WHERE id=".intval($export_file)." LIMIT 1");
        $item = $record->fetch(PDO::FETCH_ASSOC);

        $divs[] = '<div id="list-item-'.$item['id'].'" class="items listleft">'.
                $item['title'].
                '</div>';

        if($i==20) break;
    }
    $record = null;
    $dbHandle = null;

    $hr = '<div class="separator"></div>';

    print join ($hr, $divs);

    if ($offset < count($_SESSION['export_files']) - 20) {
        print '<div class="ui-state-highlight lib-shadow-top" style="margin-top:4px" title="Next">';
        print '<a href="items.php?file='.$_SESSION['export_files'][$offset+20].'" class="navigation" style="display:block;width:100%" id="file-'.$_SESSION['export_files'][$offset+20].'">';
        print '<span class="ui-icon ui-icon-triangle-1-s" style="margin:auto"></span>';
        print '</a></div>';
    }
    ?>
</div>
<div style="width:100%;height:100%;overflow:auto" id="items-right"></div>